/*
*Copyright (c) Nikhil Jain.
*Project ICARUS, communications and 
*artificial intelligence.
*/



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package javadrone.server;

//~--- non-JDK imports --------------------------------------------------------

import javadrone.Drone;
import javadrone.Server;

import javadrone.command.CommandQueue;

//~--- JDK imports ------------------------------------------------------------

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import java.util.HashMap;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Nikzzay
 */
public abstract class ServerImpl extends UnicastRemoteObject implements Server {
    public static final String            RMI_PROT = "rmi:";
    private static Context                namingContext;
    private static HashMap<String, Drone> registeredDrones;
    private String                        name;

    public ServerImpl(String name) throws RemoteException, NamingException {
        this.name     = name;
        namingContext = new InitialContext();
        namingContext.rebind(RMI_PROT + name, ServerImpl.this);
        registeredDrones = new HashMap<>();
    }

    @Override
    public String getName() throws RemoteException {
        return name;
    }

    @Override
    public Drone registerDrone(String droneName) throws RemoteException {
        System.out.println("Remote client registered...");

        DroneImpl d = new DroneImpl(new CommandQueue(), droneName);

        registeredDrones.put(droneName, d);
        hasRegistered(d);

        return d;
    }

    public Drone getDrone(String droneName) {
        return registeredDrones.get(droneName);
    }

    @Override
    public long getTime() throws RemoteException {
        return System.currentTimeMillis();
    }

    public abstract void hasRegistered(DroneImpl d);
}


//~ Formatted by Jindent --- http://www.jindent.com
